﻿using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using SPOffline.Web;

namespace SPOffline.Deployment
{
    /// <summary>
    /// Feature receiver for SharePoint User Account Control.
    /// </summary>
    [Guid("22b968af-0580-4e0a-ae68-166ec12a761a")]
    public class FeatureReceiver: SPFeatureReceiver
    {
        /// <summary>
        /// Occurs after a Feature is activated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            // merge the custom site map for breadcrumb
            SPSecurity.RunWithElevatedPrivileges(() => SPWebService.AdministrationService.ApplyApplicationContentToLocalServer());
        }

        /// <summary>
        /// Occurs when a Feature is deactivated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            // clean up jobs
            SPSecurity.RunWithElevatedPrivileges(() =>
                                                     {
                                                         foreach (var webApplication in SPWebService.ContentService.WebApplications)
                                                         {
                                                             // clean up jobs
                                                             foreach (var job in webApplication.JobDefinitions)
                                                             {
                                                                 if (job.Name == Constants.JobName)
                                                                 {
                                                                     job.Delete();
                                                                 }
                                                             }
                                                         }
                                                     });

        }

        /// <summary>
        /// Occurs after a Feature is installed.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        /// <summary>
        /// Occurs when a Feature is uninstalled.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }
    }
}